package com.mark.mall.admin.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mark.mall.admin.platform.dto.PageParamDTO;
import com.mark.mall.admin.platform.dto.param.MemberAddParam;
import com.mark.mall.admin.platform.dto.param.MemberUpdateParam;
import com.mark.mall.admin.platform.entity.Member;
import com.mark.mall.admin.platform.mapper.MemberMapper;
import com.mark.mall.admin.platform.service.MemberService;
import com.mark.mall.common.myException.MyException;
import com.mark.mall.common.resultBean.R;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-02-28
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Override
    public R<IPage<Member>> memberList(PageParamDTO param, String name, String mobile) {
        // 获取会员集合
        Page<Member> page = baseMapper.selectPage(new Page<>(param.getPageNo(), param.getPageSize()),
                Wrappers.<Member>lambdaQuery()
                        .like(StrUtil.isNotBlank(mobile), Member::getMobile, mobile)
                        .like(StrUtil.isNotBlank(name), Member::getName, name));
        return new R<>(page);
    }

    @Override
    public R<Boolean> addMember(MemberAddParam param) {
        // 对手机号校验
        if (!Validator.isMobile(param.getMobile())) {
            throw new MyException("手机号不合法,请核实!");
        }
        // 将新的会员信息覆盖老的会员信息
        Member member = BeanUtil.copyProperties(param, Member.class);
        // 创建盐值，保证密码安全
        String salt = IdWorker.getIdStr();
        member.setSalt(salt);
        // 密码使用md5加密处理
        String password = SecureUtil.md5(param.getPassword() + salt);
        member.setPassword(password);
        // 完成会员新增
        save(member);
        return new R<>(true, "新增成功！");
    }

    @Override
    public R<Boolean> delMember(String id) {
//        Member member = baseMapper.selectById(id);
//        if (member == null) {
//            throw new MyException("id为{" + id + "}的会员不存在！");
//        }
//        // 改变会员的状态，实现假删
//        member.setState(PlatformConstant.STATE_NO);
//        baseMapper.update(member, new QueryWrapper<Member>().lambda().eq(Member::getId, id));
        baseMapper.deleteById(id);
        return new R<>(true, "删除成功！");
    }

    @Override
    public R<Boolean> updateMember(MemberUpdateParam param) {
        Member member = baseMapper.selectById(param.getId());
        if (member == null) {
            throw new MyException("id为{" + param.getId() + "}的会员不存在！");
        }
        if (!Validator.isMobile(param.getMobile())) {
            throw new MyException("手机号不合法,请核实!");
        }
        BeanUtils.copyProperties(param, member);
        String password = SecureUtil.md5(param.getPassword() + member.getSalt());
        member.setPassword(password);
        member.setState("33");


       member.setMobile(null);
        // baseMapper修改密码
        //baseMapper.update(member, new QueryWrapper<Member>().lambda().eq(Member::getId, param.getId()));
        // baseMapper.updateById(member);
        // 选装件修改密码，可以赋值 null
        baseMapper.alwaysUpdateSomeColumnById(member);
        return new R<>(true, "修改成功！");
    }

}
